<template>
  <div class="login">
    <van-nav-bar class="login-vip" title="会员登录" left-arrow @click-left="$router.go(-1)" />
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input class="inp" maxlength="11" placeholder="请输入手机号码" type="text" v-model="mobile">
        </div>
        <div class="form-item">
          <input class="inp" maxlength="4" placeholder="请输入图形验证码" type="text" v-model="imgCode">
          <img :src="base64" alt="图形验证码" @click="getImgCode">
        </div>
        <div class="form-item">
          <input class="inp" placeholder="请输入短信验证码" type="text" v-model="smsCode">
          <button @click="getSmsCode" :disabled="totalSecond !== second" :class="{'forbid': totalSecond !== second}">{{ totalSecond === second ? '获取验证码' : second + `秒后重新发送`}} </button>
        </div>
      </div>

      <div class="login-btn" @click="login">登录</div>
    </div>
  </div>
</template>

<script>
import { getImgCode, getSmsCode, getUserInfo } from '@/api/login'
export default {
  name: 'LoginPage',
  data () {
    return {
      base64: '',
      key: '',
      md5: '',
      mobile: '',
      imgCode: '',
      smsCode: '',
      totalSecond: 60,
      second: 60,
      timer: null
    }
  },
  created () {
    this.getImgCode()
  },
  methods: {
    // 获取图形验证码
    async getImgCode () {
      const res = await getImgCode()
      // console.log(res)
      const { data: { base64, key, md5 } } = res
      this.base64 = base64
      this.key = key
      this.md5 = md5
    },
    // 获取短信验证码
    async getSmsCode () {
      if (!this.isValid()) return
      if (!this.timer && this.second === this.totalSecond) {
        const res = await getSmsCode(this.imgCode, this.key, this.mobile)
        // console.log(res)
        this.$toast(res.message)
        this.timer = setInterval(() => {
          this.second--
          if (this.second < 1) {
            clearInterval(this.timer)
            this.second = this.totalSecond
            this.timer = null
          }
        }, 1000)
      }
    },
    // 登录获取用户信息
    async login () {
      if (!this.isValid()) return
      // console.log(this.smsCode)
      if (!/^\d{6}$/.test(this.smsCode)) {
        this.$toast('请输入正确的验证码')
        return
      }
      const res = await getUserInfo(this.mobile, this.smsCode)
      // console.log(res)
      this.$store.commit('user/setUserInfo', res.data)
      const backUrl = this.$route.query.backUrl || '/'
      this.$router.replace(backUrl)
      this.$toast('登录成功')
    },
    // 正则表达式验证手机号与验证码
    isValid () {
      if (!/^1[3456789]\d{9}$/.test(this.mobile)) {
        this.$toast('请输入正确的手机号')
        return false
      }
      if (!/^[a-zA-Z0-9]{4}$/.test(this.imgCode)) {
        this.$toast('请输入正确的验证码')
        return false
      }
      return true
    }
  }
}
</script>

<style lang="less" scoped>
.login-vip {
  background-color: #fff;
  ::v-deep .van-nav-bar__title {
    color: black;
  }
}
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
    .forbid {
      color: #b8b8b8;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 16px;
  }
}
</style>
